﻿/*
 * zstring_unittest.cxx
 *
 *  Created on: 2015年4月8日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zalgorithm/zstring.h"
#include "zpsoe/zexception.h"

using namespace zpsoe::zalgorithm;

TEST(ZstringTest, find) {
    EXPECT_TRUE(zstring::find("abcdefg", "cd"));
    EXPECT_FALSE(zstring::find("abcdefg", "123"));
    EXPECT_FALSE(zstring::find("ABC", "abcdefg"));
}

TEST(ZstringTest, trim) {
    EXPECT_STREQ("abc", zstring::trim(" abc", " ").c_str());
    EXPECT_STREQ("abc", zstring::trim("abc ", " ").c_str());
    EXPECT_STREQ("abc", zstring::trim("abc", " ").c_str());

    EXPECT_STREQ(" abc", zstring::trim(" abc\t", "\t").c_str());
    EXPECT_STREQ("abc ", zstring::trim("\tabc ", "\t").c_str());
    EXPECT_STREQ("abc\t ", zstring::trim("abc\t ", "\t").c_str());

    EXPECT_STREQ("abc", zstring::trim("abc", " \t").c_str());
    EXPECT_STREQ("abc", zstring::trim(" abc\t ", " \t").c_str());
    EXPECT_STREQ("abc", zstring::trim("\tabc ", " \t").c_str());
    EXPECT_STREQ("abc", zstring::trim(" \tabc \t", " \t").c_str());
}

TEST(ZstringTest, to_lower) {
    EXPECT_STREQ("abc", zstring::to_lower("abc").c_str());
    EXPECT_STREQ("abc", zstring::to_lower("ABC").c_str());
}

TEST(ZstringTest, to_upper) {
    EXPECT_STREQ("ABC", zstring::to_upper("abc").c_str());
    EXPECT_STREQ("ABC", zstring::to_upper("ABC").c_str());
    EXPECT_STREQ(" ABC ", zstring::to_upper(" abc ").c_str());
}

TEST(ZstringTest, replace) {
    EXPECT_STREQ("1a111b1113c", zstring::replace("1a2b23c", "2", "111").c_str());
    EXPECT_STREQ("", zstring::replace("", "abc", "111").c_str());
    EXPECT_STREQ("abc", zstring::replace("abc", "", "111").c_str());
    EXPECT_STREQ("abc", zstring::replace("abc", "111", "111").c_str());
}

TEST(ZstringTest, erase) {
    EXPECT_STREQ("1ab3c", zstring::erase("1a22b223c", "2").c_str());
    EXPECT_STREQ("1a22b223c", zstring::erase("1a22b223c", "").c_str());
    EXPECT_STREQ("", zstring::erase("", "abc").c_str());
}

TEST(ZstringTest, is_digit) {
    EXPECT_TRUE(zstring::is_digit("123"));
    EXPECT_FALSE(zstring::is_digit("12a3"));
}

TEST(ZstringTest, is_version) {
    EXPECT_TRUE(zstring::is_version("1.2.3"));
    EXPECT_FALSE(zstring::is_version(""));
    EXPECT_FALSE(zstring::is_version("1.a.3"));
    EXPECT_FALSE(zstring::is_version(".123"));
    EXPECT_FALSE(zstring::is_version("123."));
}

TEST(ZstringTest, is_alnum) {
    EXPECT_TRUE(zstring::is_alnum("12a3"));
    EXPECT_TRUE(zstring::is_alnum("123"));
    EXPECT_FALSE(zstring::is_alnum(""));
    EXPECT_FALSE(zstring::is_alnum("12+3"));
}

TEST(ZstringTest, is_alpha) {
    EXPECT_TRUE(zstring::is_alpha("abc"));
    EXPECT_FALSE(zstring::is_alpha(""));
    EXPECT_FALSE(zstring::is_alpha("123abc"));
}

TEST(ZstringTest, string_cast) {
    const bool expected1_ = zstring::string_cast<bool>("1");
    EXPECT_TRUE(expected1_);

    const double expected2_ = zstring::string_cast<double>("2.1111");
    const double actual2_ = 2.1111;
    EXPECT_DOUBLE_EQ(actual2_, expected2_);

    const int16_t expected3_ = zstring::string_cast<int16_t>("12345");
    const int16_t actual3_ = 12345;
    EXPECT_EQ(actual3_, expected3_);

    const int32_t expected4_ = zstring::string_cast<int32_t>("1234567890");
    const int32_t actual4_ = 1234567890;
    EXPECT_EQ(actual4_, expected4_);

    const int64_t expected5_ = zstring::string_cast<int64_t>("1234567890");
    const int64_t actual5_ = 1234567890;
    EXPECT_EQ(actual5_, expected5_);

    const uint16_t expected6_ = zstring::string_cast<uint16_t>("12345");
    const uint16_t actual6_ = 12345U;
    EXPECT_EQ(actual6_, expected6_);

    const uint32_t expected7_ = zstring::string_cast<int32_t>("1234567890");
    const uint32_t actual7_ = 1234567890U;
    EXPECT_EQ(actual7_, expected7_);

    const uint64_t expected8_ = zstring::string_cast<uint64_t>("1234567890");
    const uint64_t actual8_ = 1234567890UL;
    EXPECT_EQ(actual8_, expected8_);
}

TEST(ZstringTest, to_str) {
    const string expected1_ = zstring::to_str<bool>(true);
    const string actual1_ = "1";
    EXPECT_STREQ(actual1_.c_str(), expected1_.c_str());

    const string expected2_ = zstring::to_str<double>(2.1111);
    const string actual2_ = "2.1111";
    EXPECT_STREQ(actual2_.c_str(), expected2_.c_str());

    const string expected3_ = zstring::to_str<int16_t>(12345);
    const string actual3_ = "12345";
    EXPECT_STREQ(actual3_.c_str(), expected3_.c_str());

    const string expected4_ = zstring::to_str<int32_t>(1234567890);
    const string actual4_ = "1234567890";
    EXPECT_STREQ(actual4_.c_str(), expected4_.c_str());

    const string expected5_ = zstring::to_str<int64_t>(1234567890);
    const string actual5_ = "1234567890";
    EXPECT_STREQ(actual5_.c_str(), expected5_.c_str());

    const string expected6_ = zstring::to_str<uint16_t>(12345U);
    const string actual6_ = "12345";
    EXPECT_STREQ(actual6_.c_str(), expected6_.c_str());

    const string expected7_ = zstring::to_str<uint32_t>(1234567890U);
    const string actual7_ = "1234567890";
    EXPECT_STREQ(actual7_.c_str(), expected7_.c_str());

    const string expected8_ = zstring::to_str<uint64_t>(1234567890UL);
    const string actual8_ = "1234567890";
    EXPECT_STREQ(actual8_.c_str(), expected8_.c_str());
}

TEST(ZstringTest, to_vector) {
    vector<string> v1_;
    const string s1_("a\nb+c");

    zstring::to_vector(s1_, v1_, "\n+");
    EXPECT_EQ(3, v1_.size());
    EXPECT_STREQ("a", v1_[0].c_str());
    EXPECT_STREQ("b", v1_[1].c_str());
    EXPECT_STREQ("c", v1_[2].c_str());

    vector<string> v2_;
    const string s2_("a\nb\nc\n");

    zstring::to_vector(s2_, v2_, "\n");
    EXPECT_EQ(4, v2_.size());
    EXPECT_STREQ("a", v2_[0].c_str());
    EXPECT_STREQ("b", v2_[1].c_str());
    EXPECT_STREQ("c", v2_[2].c_str());
    EXPECT_STREQ("", v2_[3].c_str());

    vector<string> v3_;
    const string s3_("\n");

    zstring::to_vector(s3_, v3_, "\n");
    EXPECT_EQ(2, v3_.size());
    EXPECT_STREQ("", v3_[0].c_str());
    EXPECT_STREQ("", v3_[1].c_str());

    vector<string> v4_;
    const string s4_("abc.com");

    zstring::to_vector(s4_, v4_, "\n");
    EXPECT_EQ(1, v4_.size());
}

TEST(ZstringTest, to_map1) {
    vector<string> v_;
    v_.push_back("a");
    v_.push_back("b");
    v_.push_back("c");

    map<string, string> m_;

    zstring::to_map(v_, m_);

    EXPECT_EQ(v_.size(), m_.size());
    EXPECT_STREQ("", m_["a"].c_str());
    EXPECT_STREQ("", m_["b"].c_str());
    EXPECT_STREQ("", m_["c"].c_str());
}

TEST(ZstringTest, to_map2) {
    map<string, string> m1_;
    const string s1_(
            "a\n"
            "b\n"
            "c\n"
            );

    zstring::to_map(s1_, m1_, "\n");
    EXPECT_EQ(4, m1_.size());
    EXPECT_STREQ("", m1_["a"].c_str());
    EXPECT_STREQ("", m1_["b"].c_str());
    EXPECT_STREQ("", m1_["c"].c_str());
    EXPECT_STREQ("", m1_[""].c_str());

    map<string, string> m2_;
    const string s2_("\n");

    zstring::to_map(s2_, m2_, "\n");
    EXPECT_EQ(1, m2_.size());
    EXPECT_STREQ("", m2_[""].c_str());

    map<string, string> m3_;
    const string s3_("");

    zstring::to_map(s3_, m3_, "\n");
    EXPECT_EQ(1, m3_.size());
    EXPECT_STREQ("", m3_[""].c_str());
}
